package lambda_expression;

import java.io.InputStream;
import java.util.*;
import java.util.function.IntConsumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Stream集合元素操作
 */
public class App7 {

    public static void main(String[] args) {
       // 1. 批量数据 -> Stream对象
        // 多个数据
        Stream stream = Stream.of("admin", "tom", "albin", "tom", "zhuzhu");
        // 转为string字符串拼接
//        String string = stream.collect(Collectors.joining()).toString();
//        System.out.println(string);
        // 转为Set集合
//        Set<String> setstr = (Set<String>)stream.collect(Collectors.toSet());
//        System.out.println(setstr);
        //转为Map
        Map<String,String> map0 = (Map<String, String>)stream.collect(Collectors.toMap(x -> x + String.valueOf(Math.random()), y -> "value:"+y));
        System.out.println(map0);


        // 数组
        String[] strArrays = new String[]{"tom","jack"};
        Stream stream1 = Arrays.stream(strArrays);
        Object[] objx = stream1.toArray(String[]::new);
        System.out.println(objx.length);

        // 列表
        List<String> list = new ArrayList<String>(){{
            add("水浒传");
            add("西游记");
            add("红楼梦");
            add("三国志");
        }};
        Stream stream2 = list.stream();
        list.stream().filter(a -> a.length()>3).collect(Collectors.toList());

        //集合
        Set<String> set = new HashSet<>();
        set.add("及时雨-宋江");
        set.add("智多星-吴用");
        set.add("花和尚-鲁智深");
        Stream stream3 = set.stream();
        set.stream().filter(a -> a.length()>6).forEach(System.out::println);


        // Map
        Map<String,Integer> map = new HashMap<>();
        map.put("tom", 1000);
        map.put("jerry", 1200);
        map.put("shuke", 1000);
        Stream stream4 = map.entrySet().stream();
        map.entrySet().stream().filter(a -> a.getKey().equals("tom")).forEach(System.out::println);
        // 2. Stream对象对于基本数据类型的功能封装
        // int / long / double
        // 只需要一次装箱拆箱
        IntStream.of(new int[]{10,20,30}).forEach(System.out::println);
        IntStream.range(1,5).forEach(System.out::println);
        // 语法糖
        IntStream.rangeClosed(1,5).forEach(System.out::println);


    }


}
